package com.tanovo.api;

import com.tanovo.bo.MenuQueryBo;
import com.tanovo.bo.ResourceCheckBo;
import com.tanovo.bo.ResultObject;
import com.tanovo.bo.UserCore;
import com.tanovo.config.UserInfoContext;
import com.tanovo.constant.ResourcesConstant;
import com.tanovo.domain.Resources;
import com.tanovo.service.IResourcesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "资源")
public class ResourceApi {

    @Autowired
    private IResourcesService iResourcesService;

    @PostMapping("/resources/add")
    @ApiOperation(value = "添加资源", notes = "添加资源")
    public ResultObject addNewUser(@RequestBody Resources Resource) {
        iResourcesService.addResource(Resource);
        return new ResultObject();
    }

    @GetMapping("/resources/delete")
    @ApiOperation(value = "删除资源", notes = "删除资源")
    public ResultObject delete(@RequestParam Integer ResourceId) {
        iResourcesService.delete(ResourceId);
        return new ResultObject();
    }

    @PostMapping("/resources/delete/list")
    @ApiOperation(value = "批量删除资源", notes = "批量删除资源")
    public ResultObject deleteList(@RequestBody List<Integer> resourceIdList) {
        iResourcesService.deleteList(resourceIdList);
        return new ResultObject();
    }

    @GetMapping("/resources/detail")
    @ApiOperation(value = "查看资源详情", notes = "查看资源详情")
    public ResultObject<Resources> detail(@RequestParam Integer ResourceId) {
        Resources detail = iResourcesService.getDetail(ResourceId);
        return new ResultObject(detail);
    }

    @PostMapping("/resources/update")
    @ApiOperation(value = "修改编辑资源", notes = "修改编辑资源")
    public ResultObject editResource(@RequestBody Resources Resource) {
        iResourcesService.editResource(Resource);
        return new ResultObject();
    }


    @PostMapping("/resources/top/list")
    @ApiOperation(value = "顶级资源列表", notes = "顶级资源列表")
    public ResultObject<List<Resources>> getTopList() {
        UserCore user = UserInfoContext.getUser();
        List<Resources> list = iResourcesService.getAppResourceByParentId(ResourcesConstant.RESOURCE_ROOT,user.getClientId(),user.getId());
        return new ResultObject(list);
    }

    @GetMapping("/resources/child/list")
    @ApiOperation(value = "获取子资源列表", notes = "获取子资源列表")
    public ResultObject<List<Resources>> getChildList(@RequestParam Integer parentId) {
        UserCore user = UserInfoContext.getUser();
        List<Resources> list = iResourcesService.getAppResourceByParentId(parentId,user.getClientId(),user.getId());
        return new ResultObject(list);
    }

    @GetMapping("/resources/currentUser/list")
    @ApiOperation(value = "当前登录用户(按钮)权限列表（用来检查前端是否有权限）", notes = "当前登录用户(按钮)权限列表")
    public ResultObject<List<Resources>> getCurrentUserList() {
        UserCore user = UserInfoContext.getUser();
        List<Resources> list = iResourcesService.getCurrentUserList(user.getId());
        return new ResultObject(list);
    }

    @GetMapping("/resources/currentUser/menuList")
    @ApiOperation(value = "当前用户菜单列表", notes = "当前用户菜单列表")
    public ResultObject<List<Resources>> getCurrentUserMenuList() {
        //判断当前用户是不是超管 如果是 走展示所有可用菜单接口
        List<Resources> list = null;
        UserCore user = UserInfoContext.getUser();
        if(ResourcesConstant.SUPER_ADMIN_ID==user.getId()){
            list  = iResourcesService.getCurrentSystemMenuList(ResourcesConstant.RESOURCE_ROOT);
        }else {
            MenuQueryBo queryBo =new MenuQueryBo();
            queryBo.setParentId(ResourcesConstant.RESOURCE_ROOT);
            queryBo.setClientId(user.getClientId());
            queryBo.setUserId(user.getId());
            queryBo.setCompanyId(user.getCompanyId());
            list  = iResourcesService.getCurrentUserMenuList(queryBo);
        }
        return new ResultObject(list);
    }

    @GetMapping("/resources/checked/list")
    @ApiOperation(value = "当前角色的权限列表（包括选择和未选中的）", notes = "当前角色的权限列表（包括选择和未选中的）parentId=0 获取顶级资源")
    public ResultObject<List<Resources>> getCheckedList(@RequestParam Integer parentId, @RequestParam Integer roleId) {
        UserCore user = UserInfoContext.getUser();
        List<Resources> list = iResourcesService.getCheckedList(parentId, roleId,user.getClientId(),user.getId());
        return new ResultObject(list);
    }

    @GetMapping("/resources/checkPath")
    @ApiOperation(value = "检查此路径在当前用户是否存在权限", notes = "检查此路径在当前用户是否存在权限")
    public ResultObject<ResourceCheckBo> checkPath(@RequestParam String uri) {
        ResourceCheckBo resourceCheckBo=  iResourcesService.checkPath(uri);
        return new ResultObject(resourceCheckBo);
    }

    @GetMapping("/resources/admin/list")
    @ApiOperation(value = "管理员资源列表", notes = "管理员资源列表")
    public ResultObject<List<Resources>> adminResourceList() {
        List<Resources> list = iResourcesService.adminResourceList();
        return new ResultObject(list);
    }


}
